#!/bin/sh
#This script is only for mac app deployment.
#After app compiled under mac, we need create a bundle contains needed QtFrameworks

#This script will be copied to dist dir, you should run it in that dir.

DEPLOY_FRAMEWORKS_PATH="abhidhaana.app/Contents/Frameworks"
DEPLOY_PLATFORM_PLUGIN_PATH="abhidhaana.app/Contents/MacOS/platforms"
QT_LIB_PATH="/Users/cjacker/Qt5.1.1/5.1.1/clang_64/lib/"
QT_PLATFORM_PLUGIN_PATH="/Users/cjacker/Qt5.1.1/5.1.1/clang_64/plugins/platforms/"

#copy Qt platform plugin to app dir.
mkdir -p $DEPLOY_PLATFORM_PLUGIN_PATH
cp $QT_PLATFORM_PLUGIN_PATH/libqcocoa.dylib $DEPLOY_PLATFORM_PLUGIN_PATH

#copy related frameworks to app dir.
#change the Frameworks ID.

mkdir -p $DEPLOY_FRAMEWORKS_PATH
for f in QtCore QtGui QtXml QtWidgets QtNetwork QtPrintSupport
do
cp -R $QT_LIB_PATH/$f.framework $DEPLOY_FRAMEWORKS_PATH
rm -rf $DEPLOY_FRAMEWORKS_PATH/$f.framework/*.prl
rm -rf $DEPLOY_FRAMEWORKS_PATH/$f.framework/Headers
rm -rf $DEPLOY_FRAMEWORKS_PATH/$f.framework/*_debug
rm -rf $DEPLOY_FRAMEWORKS_PATH/$f.framework/Versions/5/Headers
rm -rf $DEPLOY_FRAMEWORKS_PATH/$f.framework/Versions/5/*_debug
install_name_tool -id @executable_path/../Frameworks/$f.framework/Versions/5/$f abhidhaana.app/Contents/Frameworks/$f.framework/Versions/5/$f
done

#change the binary dependencies
for binary in abhidhaana.app/Contents/MacOS/abhidhaana \
    abhidhaana.app/Contents/MacOS/platforms/libqcocoa.dylib \
    abhidhaana.app/Contents/MacOS/plugins/libmoniersedict.dylib \
    abhidhaana.app/Contents/MacOS/plugins/libmonieresdict.dylib \
    abhidhaana.app/Contents/MacOS/plugins/libqqdict.dylib \
    abhidhaana.app/Contents/Frameworks/QtWidgets.framework/Versions/5/QtWidgets \
    abhidhaana.app/Contents/Frameworks/QtCore.framework/Versions/5/QtCore \
    abhidhaana.app/Contents/Frameworks/QtGui.framework/Versions/5/QtGui \
    abhidhaana.app/Contents/Frameworks/QtNetwork.framework/Versions/5/QtNetwork \
    abhidhaana.app/Contents/Frameworks/QtXml.framework/Versions/5/QtXml \
    abhidhaana.app/Contents/Frameworks/QtPrintSupport.framework/Versions/5/QtPrintSupport
do
    for framework in QtCore QtGui QtXml QtWidgets QtNetwork QtPrintSupport
    do
    install_name_tool -change /Users/cjacker/Qt5.1.1/5.1.1/clang_64/lib/$framework.framework/Versions/5/$framework @executable_path/../Frameworks/$framework.framework/Versions/5/$framework $binary
    done
done
